package com.example.demo.generator.scaffold.registry;

import com.example.demo.generator.scaffold.model.TemplateContext;
import com.example.demo.generator.scaffold.model.TemplateDescriptor;
import com.example.demo.generator.scaffold.util.ScaffoldNameUtils;

import java.util.List;

/**
 * FISCO 公共骨架模板。
 */
public final class FiscoBaseTemplates {

    private FiscoBaseTemplates() {
    }

    public static List<TemplateDescriptor> all() {
        return List.of(
                blockchainConfig(),
                fiscoProperties(),
                blockchainService(),
                blockchainServiceImpl(),
                blockchainData(),
                abiPlaceholder(),
                binPlaceholder()
        );
    }

    private static TemplateDescriptor blockchainConfig() {
        return TemplateDescriptor.builder()
                .id("fisco-config")
                .templatePath("scaffold/templates/blockchain/base/config/BlockchainConfig.ftl")
                .outputPathResolver(context -> buildBasePath(context, "config", "BlockchainConfig.java"))
                .build();
    }

    private static TemplateDescriptor fiscoProperties() {
        return TemplateDescriptor.builder()
                .id("fisco-properties")
                .templatePath("scaffold/templates/blockchain/base/config/FiscoProperties.ftl")
                .outputPathResolver(context -> buildBasePath(context, "config", "FiscoProperties.java"))
                .build();
    }

    private static TemplateDescriptor blockchainService() {
        return TemplateDescriptor.builder()
                .id("fisco-service")
                .templatePath("scaffold/templates/blockchain/base/service/BlockchainService.ftl")
                .outputPathResolver(context -> buildBasePath(context, "service", "BlockchainService.java"))
                .build();
    }

    private static TemplateDescriptor blockchainServiceImpl() {
        return TemplateDescriptor.builder()
                .id("fisco-service-impl")
                .templatePath("scaffold/templates/blockchain/base/service/impl/BlockchainServiceImpl.ftl")
                .outputPathResolver(context -> buildBasePath(context, "service/impl", "BlockchainServiceImpl.java"))
                .build();
    }

    private static TemplateDescriptor blockchainData() {
        return TemplateDescriptor.builder()
                .id("fisco-model")
                .templatePath("scaffold/templates/blockchain/base/model/BlockchainData.ftl")
                .outputPathResolver(context -> buildBasePath(context, "model", "BlockchainData.java"))
                .build();
    }

    private static TemplateDescriptor abiPlaceholder() {
        return TemplateDescriptor.builder()
                .id("fisco-abi-readme")
                .templatePath("scaffold/templates/blockchain/base/resources/abi/README.ftl")
                .outputPathResolver(context -> ScaffoldNameUtils.deriveArtifactId(context) + "/src/main/resources/abi/README.md")
                .build();
    }

    private static TemplateDescriptor binPlaceholder() {
        return TemplateDescriptor.builder()
                .id("fisco-bin-readme")
                .templatePath("scaffold/templates/blockchain/base/resources/bin/README.ftl")
                .outputPathResolver(context -> ScaffoldNameUtils.deriveArtifactId(context) + "/src/main/resources/bin/README.md")
                .build();
    }

    private static String buildBasePath(TemplateContext context, String layer, String fileName) {
        String projectRoot = ScaffoldNameUtils.deriveArtifactId(context);
        String base = context.getBasePackage().replace('.', '/');
        String layerPath = layer.replace('.', '/');
        return projectRoot + "/src/main/java/" + base + "/fisco/" + layerPath + "/" + fileName;
    }
}
